package com.lambert.gingko.watermark.service;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import com.lambert.gingko.watermark.enums.FileType;
import com.lambert.gingko.watermark.factory.WatermarkStrategyFactory;
import com.lambert.gingko.watermark.model.WatermarkConfig;
import com.lambert.gingko.watermark.postprocessor.WatermarkPostProcessor;
import com.lambert.gingko.watermark.strategy.WatermarkProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * 水印服务类，对外提供统一接口
 * @author lambert
 * @since  2025/08/31
 */
@Slf4j
@Service
public class WatermarkService {

    private final WatermarkStrategyFactory strategyFactory;

    @Autowired
    public WatermarkService(WatermarkStrategyFactory strategyFactory) {
        this.strategyFactory = strategyFactory;
    }

    /**
     * 处理文件并添加水印
     */
    public void processFile(File file, WatermarkConfig config, WatermarkPostProcessor postProcessor) throws Exception {
        String extension = FileNameUtil.extName(file.getName());
        processFile(file, extension, config, postProcessor);
    }

    /**
     * 处理文件并添加水印（指定文件类型）
     */
    public void processFile(File file, String fileType, WatermarkConfig config, WatermarkPostProcessor postProcessor) throws Exception {
        try (FileInputStream inputStream = new FileInputStream(file)) {
            processInputStream(inputStream,file.getName(), fileType, config, postProcessor);
        }
    }

    /**
     * 处理输入流并添加水印
     */
    public void processInputStream(InputStream inputStream,String fileName, String fileType,
                                  WatermarkConfig config, WatermarkPostProcessor postProcessor) throws Exception {
        // 获取合适的处理器
        WatermarkProcessor processor = strategyFactory.getProcessor(fileType);
        
        if (processor == null) {
            String errorMsg = "不支持的文件类型或该类型处理器已禁用: " + fileType;
            log.error(errorMsg);
            throw new UnsupportedOperationException(errorMsg);
        }
        
        // 处理文件
        File processedFile = processor.process(inputStream, fileName, config);
        
        // 执行后置处理
        if (postProcessor != null) {
            postProcessor.process(processedFile, fileType);
        } else {
            FileUtil.del(processedFile);
        }
    }

    /**
     * 动态启用指定类型的处理器
     */
    public void enableProcessor(FileType type) {
        strategyFactory.enableProcessor(type);
        log.info("已启用水印处理器: {}", type.getTypeName());
    }

    /**
     * 动态禁用指定类型的处理器
     */
    public void disableProcessor(FileType type) {
        strategyFactory.disableProcessor(type);
        log.info("已禁用水印处理器: {}", type.getTypeName());
    }

    /**
     * 检查处理器是否启用
     */
    public boolean isProcessorEnabled(FileType type) {
        return strategyFactory.isProcessorEnabled(type);
    }
}
    